﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DALL.SysManage
{
    public class T_PUB_USER
    {
        public T_PUB_USER()
        { }

        /// <summary>
        /// 智能提示获取用户姓名
        /// </summary>
        /// <returns></returns>
        public DataSet GetUserName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT USER_NAME FROM T_PUB_USER ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得用户信息包含所属店所属部门用户角色
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetUserInfoByRole(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT u.NAME            AS uname, u.USER_NAME as sysmbol,\n");
            varname1.Append("       r.id         AS id, \n");
            varname1.Append("       d.DEPARTMENT_NAME AS dNmae, \n");
            varname1.Append("       t.BRANCH_NAME     AS tNmam \n");
            varname1.Append("FROM   T_PUB_USER u, \n");
            varname1.Append("       T_PUB_BRANCH t, \n");
            varname1.Append("       T_PUB_DEPARTMENT d, \n");
            varname1.Append("       T_PUB_USERROLE r \n");
            varname1.Append("WHERE  r.USER_ID=u.USER_ID and u.BRANCH_ID = t.BRANCH_ID \n");
            varname1.Append("       AND d.DEPARTMENT_ID = u.DEPARTMENT_ID and ISUSED = 1 ");

            if (strWhere.Trim() != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());

        }
        /// <summary>
        /// 获得用户信息包含所属店所属部门
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetUserInfo(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT u.NAME            AS uname, u.USER_NAME as sysmbol,\n");
            varname1.Append("       u.USER_ID         AS id, \n");
            varname1.Append("       d.DEPARTMENT_NAME AS dNmae, \n");
            varname1.Append("       t.BRANCH_NAME     AS tNmam \n");
            varname1.Append("FROM   T_PUB_USER u, \n");
            varname1.Append("       T_PUB_BRANCH t, \n");
            varname1.Append("       T_PUB_DEPARTMENT d \n");
            varname1.Append("WHERE  u.BRANCH_ID = t.BRANCH_ID \n");
            varname1.Append("       AND d.DEPARTMENT_ID = u.DEPARTMENT_ID and ISUSED = 1 ");

            if (strWhere.Trim() != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());

        }

        /// <summary>
        /// 通过用户名查找用户用于登入模块
        /// </summary>
        /// <param name="UserSymbol"></param>
        /// <returns></returns>
        public Model.SysManage.T_PUB_USER GetModelByUserSymbol(string UserSymbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USER_ID,USER_NUMID,USER_NAME,NAME,ORG_ID,PASSWORD,TIME,BRANCH_ID,DEPARTMENT_ID,POST,PHONE,MAILBOX,ISUSED from T_PUB_USER ");
            strSql.Append(" where USER_NAME=@USER_NAME and ISUSED=1");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "USER_NAME", DbType.String, UserSymbol);
            Model.SysManage.T_PUB_USER model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_USER model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_USER(");
            strSql.Append("USER_NUMID,USER_NAME,NAME,ORG_ID,PASSWORD,TIME,BRANCH_ID,DEPARTMENT_ID,POST,PHONE,MAILBOX,ISUSED)");

            strSql.Append(" values (");
            strSql.Append("@USER_NUMID,@USER_NAME,@NAME,@ORG_ID,@PASSWORD,@TIME,@BRANCH_ID,@DEPARTMENT_ID,@POST,@PHONE,@MAILBOX,@ISUSED)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "USER_NUMID", DbType.String, model.USER_NUMID);
            db.AddInParameter(dbCommand, "USER_NAME", DbType.String, model.USER_NAME);
            db.AddInParameter(dbCommand, "NAME", DbType.String, model.NAME);
            db.AddInParameter(dbCommand, "ORG_ID", DbType.Int32, model.ORG_ID);
            db.AddInParameter(dbCommand, "PASSWORD", DbType.String, model.PASSWORD);
            db.AddInParameter(dbCommand, "TIME", DbType.DateTime, model.TIME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "DEPARTMENT_ID", DbType.Int32, model.DEPARTMENT_ID);
            db.AddInParameter(dbCommand, "POST", DbType.String, model.POST);
            db.AddInParameter(dbCommand, "PHONE", DbType.String, model.PHONE);
            db.AddInParameter(dbCommand, "MAILBOX", DbType.String, model.MAILBOX);
            db.AddInParameter(dbCommand, "ISUSED", DbType.Boolean, model.ISUSED);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_USER model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_USER set ");
            strSql.Append("USER_NUMID=@USER_NUMID,");
            strSql.Append("USER_NAME=@USER_NAME,");
            strSql.Append("NAME=@NAME,");
            strSql.Append("ORG_ID=@ORG_ID,");
            strSql.Append("PASSWORD=@PASSWORD,");
            strSql.Append("TIME=@TIME,");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("DEPARTMENT_ID=@DEPARTMENT_ID,");
            strSql.Append("POST=@POST,");
            strSql.Append("PHONE=@PHONE,");
            strSql.Append("MAILBOX=@MAILBOX,");
            strSql.Append("ISUSED=@ISUSED");
            strSql.Append(" where USER_ID=@USER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "USER_ID", DbType.Int32, model.USER_ID);
            db.AddInParameter(dbCommand, "USER_NUMID", DbType.String, model.USER_NUMID);
            db.AddInParameter(dbCommand, "USER_NAME", DbType.String, model.USER_NAME);
            db.AddInParameter(dbCommand, "NAME", DbType.String, model.NAME);
            db.AddInParameter(dbCommand, "ORG_ID", DbType.Int32, model.ORG_ID);
            db.AddInParameter(dbCommand, "PASSWORD", DbType.String, model.PASSWORD);
            db.AddInParameter(dbCommand, "TIME", DbType.DateTime, model.TIME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "DEPARTMENT_ID", DbType.Int32, model.DEPARTMENT_ID);
            db.AddInParameter(dbCommand, "POST", DbType.String, model.POST);
            db.AddInParameter(dbCommand, "PHONE", DbType.String, model.PHONE);
            db.AddInParameter(dbCommand, "MAILBOX", DbType.String, model.MAILBOX);
            db.AddInParameter(dbCommand, "ISUSED", DbType.Boolean, model.ISUSED);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int USER_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_USER ");
            strSql.Append(" where USER_ID=@USER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "USER_ID", DbType.Int32, USER_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_USER GetModel(int USER_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USER_ID,USER_NUMID,USER_NAME,NAME,ORG_ID,PASSWORD,TIME,BRANCH_ID,DEPARTMENT_ID,POST,PHONE,MAILBOX,ISUSED from T_PUB_USER ");
            strSql.Append(" where USER_ID=@USER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "USER_ID", DbType.Int32, USER_ID);
            Model.SysManage.T_PUB_USER model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USER_ID,USER_NUMID,USER_NAME,NAME,ORG_ID,PASSWORD,TIME,BRANCH_ID,DEPARTMENT_ID,POST,PHONE,MAILBOX,ISUSED ");
            strSql.Append(" FROM T_PUB_USER ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_USER> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USER_ID,USER_NUMID,USER_NAME,NAME,ORG_ID,PASSWORD,TIME,BRANCH_ID,DEPARTMENT_ID,POST,PHONE,MAILBOX,ISUSED ");
            strSql.Append(" FROM T_PUB_USER ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_USER> list = new List<Model.SysManage.T_PUB_USER>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_USER ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_USER model = new Model.SysManage.T_PUB_USER();
            object ojb;
            ojb = dataReader["USER_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.USER_ID = (int)ojb;
            }
            model.USER_NUMID = dataReader["USER_NUMID"].ToString();
            model.USER_NAME = dataReader["USER_NAME"].ToString();
            model.NAME = dataReader["NAME"].ToString();
            ojb = dataReader["ORG_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ORG_ID = (int)ojb;
            }
            model.PASSWORD = dataReader["PASSWORD"].ToString();
            ojb = dataReader["TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TIME = (DateTime)ojb;
            }
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            ojb = dataReader["DEPARTMENT_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DEPARTMENT_ID = (int)ojb;
            }
            model.POST = dataReader["POST"].ToString();
            model.PHONE = dataReader["PHONE"].ToString();
            model.MAILBOX = dataReader["MAILBOX"].ToString();
            ojb = dataReader["ISUSED"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ISUSED = (bool)ojb;
            }
            return model;
        }
    }
}
